package com.ruoyi.system.service.impl;


import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

import cn.wildfirechat.pojos.*;
import cn.wildfirechat.sdk.AdminConfig;
import cn.wildfirechat.sdk.GroupAdmin;
import cn.wildfirechat.sdk.RobotService;
import cn.wildfirechat.sdk.UserAdmin;
import cn.wildfirechat.sdk.model.IMResult;
import com.ruoyi.common.tools.RateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TRobotMapper;
import com.ruoyi.system.domain.TRobot;
import com.ruoyi.system.service.ITRobotService;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;

/**
 * 机器人Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-18
 */
@Service
@Slf4j
public class TRobotServiceImpl implements ITRobotService 
{
    @Autowired
    private TRobotMapper tRobotMapper;

    @Value("${admin_url:http://localhost:18080}")
    private String adminUrl;

    @Value("${admin_secret:123456}")
    private String adminSecret;

    private RateLimiter rateLimiter;

    @PostConstruct
    private void init() {
        log.info("adminUrl========="+adminUrl);
        log.info("adminSecret==========="+adminSecret);
        AdminConfig.initAdmin(adminUrl, adminSecret);
        rateLimiter = new RateLimiter(60, 200);
    }

    /**
     * 查询机器人
     * 
     * @param id 机器人主键
     * @return 机器人
     */
    @Override
    public TRobot selectTRobotById(Long id)
    {
        TRobot tRobot = tRobotMapper.selectTRobotById(id);

        return tRobotMapper.selectTRobotById(id);
    }

    /**
     * 查询机器人列表
     * 
     * @param tRobot 机器人
     * @return 机器人
     */
    @Override
    public List<TRobot> selectTRobotList(TRobot tRobot)
    {
        List<TRobot> tRobots = tRobotMapper.selectTRobotList(tRobot);
        for (TRobot robot : tRobots) {
            if (robot.getDt() != null){
                LocalDateTime ldt =
                    LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.valueOf(robot.getDt())), ZoneId.systemDefault());
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                robot.setDt(ldt.format(fmt));
            }
        }
        return tRobots;
    }

    /**
     * 新增机器人
     * 
     * @param tRobot 机器人
     * @return 结果
     */
    @Override
    public Boolean insertTRobot(TRobot tRobot) throws Exception {
        InputCreateRobot inputCreateRobot = new InputCreateRobot();
        BeanUtils.copyProperties(tRobot,inputCreateRobot);
        UserAdmin.createRobot(inputCreateRobot);
        return true;
    }

    /**
     * 修改机器人
     * 
     * @param tRobot 机器人
     * @return 结果
     */
    @Override
    public int updateTRobot(TRobot tRobot)
    {
        InputCreateRobot inputCreateRobot = new InputCreateRobot();
        BeanUtils.copyProperties(tRobot,inputCreateRobot);
        try {
            UserAdmin.createRobot(inputCreateRobot);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return tRobotMapper.updateTRobot(tRobot);
    }

    /**
     * 批量删除机器人
     * 
     * @param ids 需要删除的机器人主键
     * @return 结果
     */
    @Override
    public int deleteTRobotByIds(Long[] ids)
    {
        return tRobotMapper.deleteTRobotByIds(ids);
    }

    /**
     * 删除机器人信息
     * 
     * @param id 机器人主键
     * @return 结果
     */
    @Override
    public int deleteTRobotById(Long id)
    {
        try {
            UserAdmin.destroyRobot(id.toString());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 1;
    }

    @Override
    public Integer getCount() {
        return tRobotMapper.getCount();
    }
}
